package com.cskaoyan.listener;

import com.cskaoyan.utils.Constant;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*;

/**
 * @Author Pudding
 * @Date 2024/5/27 21:37
 */
@WebListener
public class ResourceInitServletContextListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        sharePath(servletContextEvent); //提出配置文件中的共享路径

        shareVendors(servletContextEvent); //加载物流信息配置文件

        initBeanUtils();
    }

    private void shareVendors(ServletContextEvent servletContextEvent) {
        InputStream inputStream = ResourceInitServletContextListener.class.getClassLoader().getResourceAsStream("express_vendors.properties");
        Properties properties = new Properties();
        try {
            properties.load(new InputStreamReader(inputStream, "utf-8"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        List<Map<String, String>> vendors = new ArrayList<>();
        Enumeration<?> enumeration = properties.propertyNames();
        while (enumeration.hasMoreElements()) {
            String key = (String) enumeration.nextElement();
            String value = properties.getProperty(key);
            HashMap<String, String> map = new HashMap<>();
            map.put("code", key);
            map.put("name", value);
            vendors.add(map);
        }
        //让这个供应商在应用中共享
        ServletContext servletContext = servletContextEvent.getServletContext();
        servletContext.setAttribute(Constant.VENDORS, vendors);
    }

    private void initBeanUtils() {
        //Converter(value) 中有参构造方法的参数 提供的是转换过程中的默认值
        DateConverter converter = new DateConverter(null);
        //将字符串转换为Date的使用，采用的pattern(格式)
        converter.setPatterns(new String[]{"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss"});
        ConvertUtils.register(converter, Date.class);
    }

    private static void sharePath(ServletContextEvent servletContextEvent) {
        InputStream inputStream = ResourceInitServletContextListener.class.getClassLoader().getResourceAsStream("parameter.properties");
        Properties properties = new Properties();
        try {
            properties.load(inputStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        String storagePath = properties.getProperty(Constant.STORAGE_PATH);

        //让这个路径在应用中共享
        ServletContext servletContext = servletContextEvent.getServletContext();
        servletContext.setAttribute(Constant.STORAGE_PATH, storagePath);
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        System.out.println("应用程序关闭的时候执行");
    }
}
